package com.example.makerspace.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.makerspace.domain.UserProjectDO;
import com.example.makerspace.mapper.UserProjectMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;

@Component
public class UserProjectDAO extends  BaseDAO<UserProjectDO>{
    @Resource
    private UserProjectMapper userProjectMapper;

    @PostConstruct
    public void init(){
        this.mapper = userProjectMapper;
    }

    public IPage<UserProjectDO> selectByPage(Page<UserProjectDO> page, String name, String username, String praise) {
        LambdaQueryWrapper<UserProjectDO> qw = new LambdaQueryWrapper<UserProjectDO>();
        qw.eq(true, UserProjectDO::getVisibility, "public").and(wrapper ->
                        wrapper.like(StringUtils.isNotEmpty(name),UserProjectDO::getProjectName,name).or()
                                .like(StringUtils.isNotEmpty(name),UserProjectDO::getProjectDescription,name)
                                .eq(StringUtils.isNotEmpty(username),UserProjectDO::getProjectDescription,username));
        if(praise.equals("1")){
         qw.orderBy(true,false,UserProjectDO::getPraiseNumber,UserProjectDO::getCreateTime);
        }
       else if(praise.equals("0")){
            qw.orderBy(true,true,UserProjectDO::getPraiseNumber,UserProjectDO::getCreateTime);
        }else {
           qw.orderByDesc(UserProjectDO::getCreateTime);
        }
        return  mapper.selectPage(page,qw);
    }

    public void deleteByProjectId(int projectId) {
        QueryWrapper<UserProjectDO> wrapper = new QueryWrapper<>();
        wrapper.eq("project_id",projectId);
         mapper.delete(wrapper);
    }

    public List<UserProjectDO> selectByName(String projectName) {
        QueryWrapper<UserProjectDO> wrapper = new QueryWrapper<>();
        wrapper.eq("project_name",projectName);
        return  mapper.selectList(wrapper);
    }

    public UserProjectDO selectByProjectId(Integer projectId) {
        QueryWrapper<UserProjectDO> wrapper = new QueryWrapper<>();
        wrapper.eq("project_id",projectId);
        return  mapper.selectOne(wrapper);

    }

    public List<UserProjectDO> selectByPersonProject(String username) {
        QueryWrapper<UserProjectDO> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);
        return  mapper.selectList(wrapper);
    }

    public List<UserProjectDO> selectByPersonProjectAndName(String username, String name) {
        QueryWrapper<UserProjectDO> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username).like("project_name",name);
        return  mapper.selectList(wrapper);
    }

    public List<UserProjectDO> selectByOrderWeek() {
        QueryWrapper<UserProjectDO> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("week_view_number");
        wrapper.last("limit 0,10");
        return  mapper.selectList(wrapper);
    }

    public List<UserProjectDO> selectByOrderMonth() {
        QueryWrapper<UserProjectDO> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("month_view_number");
        wrapper.last("limit 0,10");
        return  mapper.selectList(wrapper);
    }

    public IPage<UserProjectDO> selectByPageNoContition(Page<UserProjectDO> page, String praise) {
        QueryWrapper<UserProjectDO> wrapper = new QueryWrapper<>();
        if(praise.equals("1")){
            wrapper.orderByDesc("praise_number").orderByDesc("create_time");
        }else {
            wrapper.orderByAsc("praise_number").orderByDesc("create_time");
        }
        return  mapper.selectPage(page,wrapper);
    }

    public IPage<UserProjectDO> selectByPageNO(Page<UserProjectDO> page) {
        QueryWrapper<UserProjectDO> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("create_time");
        return  mapper.selectPage(page,wrapper);
    }

    public List<UserProjectDO> selectInProjectId(List<Integer> projectIds) {
        QueryWrapper<UserProjectDO> wrapper = new QueryWrapper<>();
        wrapper.in("project_id",projectIds);
        return  mapper.selectList(wrapper);
    }
}
